package com.example.controller;
import com.example.pojo.Resume;
import com.example.service.IEducationExperienceService;
import com.example.service.IResumeClassifyService;
import com.example.service.IResumeService;
import com.example.service.IWorkExperienceService;
import com.example.util.Base64Util;
import com.example.util.DateUtils;
import com.example.util.GetResourcesFile;
import com.example.util.IdUtils;
import com.example.vo.ResultVo;
import com.example.vo.per.ResuSelAllPage;
import com.example.vo.per.RsuAndresuDetails;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 简历表  前端控制器
 * </p>
 *
 * @author wsl
 * @since 2022-04-11
 */
@RestController
@RequestMapping("/emp/resume")
@Api(tags = "简历管理",value = "")
@CrossOrigin(allowCredentials = "true")
public class ResumeController {

    @Autowired
    private IResumeService irs;
    @Autowired
    private IResumeClassifyService ircs;
    @Autowired
    private IWorkExperienceService iwes;
    @Autowired
    private IEducationExperienceService iees;

    @Autowired
    private GetResourcesFile getResourcesFile;

    @PostMapping("/image")
    public String insImg(MultipartFile file) throws IOException {
        String upload = upload(file, getResourcesFile.getResourcesFile().replace("1.txt", ""));
        // 后台
        Resource fafa = getResourcesFile.getResources(upload);
        File file1 = fafa.getFile();
        // 前台
        String s = "data:image/png;base64," +  Base64Util.fileToBase64(file1);
        return s;
    }

    public String upload(MultipartFile file, String path) {
        String s = new IdUtils().getIncreaseIdByNanoTime();
        String filename = null;
        if(!file.isEmpty()) {
            //上传文件名
            filename = s + file.getOriginalFilename();

            File filepath = new File(path,filename);
            //判断路径是否存在，如果不存在就创建一个
            if (!filepath.getParentFile().exists()) {
                filepath.getParentFile().mkdirs();
            }
            //将上传文件保存到一个目标文件当中
            try {
                file.transferTo(new File(path + File.separator + filename));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return filename;
    }


    @PostMapping("/sav")
    @ApiOperation("新增简历")
    @Transactional(rollbackFor = Exception.class)
    public ResultVo insResu(@RequestBody RsuAndresuDetails rad){
        System.err.println("----- > " + rad.getResume().getBirth());
        Integer rid = irs.insRes(rad.getResume());
        if (rid != null){
            ircs.insResc(rad.getResumeClassify(),rid);
            iwes.insWorks(rad.getWorkExperience(),rid);
            iees.insEEs(rad.getEducationExperience(),rid);
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取个人简历信息")
    public ResultVo getResumeByEmpId(@PathVariable("id")Integer empId){
        return irs.getResumeByEmpId(empId);
    }

    @PostMapping("/batchsend")
    @ApiOperation(value = "批量修改 - 发送邮件")
    public ResultVo batchSend(@RequestBody List<Integer> list) {
        List<Resume> ids = irs.selIds(list);
        if (!ids.isEmpty()) {
            return ResultVo.success("邮件发送成功！");
        } else {
            return ResultVo.error("网络连接异常！");
        }
    }

    @GetMapping("/selall")
    @ApiOperation(value = "获取所有简历")
    public ResultVo getResumeOptions(ResuSelAllPage resuSelAllPage){
        System.err.println("resuSelAllPage --> " + resuSelAllPage);
        PageInfo<Resume> info = irs.selrsall(resuSelAllPage);
        return ResultVo.response(info);
    }

    @DeleteMapping("/rejectOp")
    @ApiOperation("驳回应聘书")
    public ResultVo rejectOpertion(@RequestParam("ids") Integer[] ids) {
        irs.rejectO(ids);
        return ResultVo.success("成功！");
    }

    @GetMapping("/options-window")
    @ApiOperation(value = "获取简历选择窗口数据")
    public ResultVo getResumeWindowData(){
        return irs.getResumeWindowData();
    }

    @ApiOperation(value = "修改员工简历")
    @PutMapping("/update")
    public ResultVo updateEmpResume(@RequestBody Resume resume){
        if (irs.updateById(resume)) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }


    @DeleteMapping("")
    @ApiOperation("删除简历")
    public ResultVo delschedule(@RequestBody Integer[] ids){
        irs.delResuById(ids);
        return ResultVo.ok();
    }
}
